local sk__guoshi = fk.CreateSkill {

  name = "sk__guoshi",

  tags = {  },

}


sk__guoshi:addEffect(fk.EventPhaseStart, {
  name = "sk__guoshi",
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__guoshi.name) then
      if target.phase == Player.Start then
        return true
      elseif target.phase == Player.Finish and not target.dead then
        local turn_event = player.room.logic:getCurrentEvent():findParent(GameEvent.Turn, true)
        if turn_event == nil then return end
        return #player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function (e)
          for _, move in ipairs(e.data) do
            if move.moveReason == fk.ReasonDiscard or move.moveReason == fk.ReasonJudge then
              for _, info in ipairs(move.moveInfo) do
                if player.room:getCardArea(info.cardId) == Card.DiscardPile then
                  return true
                end
              end
            end
          end
        end, Player.HistoryTurn) > 0
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if target.phase == Player.Start then
      return player.room:askForSkillInvoke(player, sk__guoshi.name)
    elseif target.phase == Player.Finish then
      return player.room:askForSkillInvoke(player, sk__guoshi.name, nil, "#sk__guoshi-invoke::"..target.id)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if target.phase == Player.Start then
      local result = room:askForArrangeCards(player, sk__guoshi.name, {room:getNCards(2), "Top", "Bottom"}, "#sk__guoshi", false, 0,
        {2, 2}, {0, 0})
      local top, bottom = result[1], result[2]
      for i = #top, 1, -1 do
        table.insert(room.draw_pile, 1, top[i])
      end
      for i = 1, #bottom, 1 do
        table.insert(room.draw_pile, bottom[i])
      end
      room:sendLog{
        type = "#GuanxingResult",
        from = player.id,
        arg = #top,
        arg2 = #bottom,
      }
    elseif target.phase == Player.Finish then
      local cards = {}
      room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function (e)
        for _, move in ipairs(e.data) do
          if move.moveReason == fk.ReasonDiscard or move.moveReason == fk.ReasonJudge then
            for _, info in ipairs(move.moveInfo) do
              if room:getCardArea(info.cardId) == Card.DiscardPile then
                table.insertIfNeed(cards, info.cardId)
              end
            end
          end
        end
      end, Player.HistoryTurn)
      local cards, _ = room:askToChooseCardsAndChoice(player, {
        cards = cards,
        choices = { "OK" },
        skill_name = sk__guoshi.name,
        prompt = "#sk__guoshi-card::" .. target.id,
        cancel_choices = {},
        min = 1,
        max = 1,
      })
      room:moveCardTo(cards, Card.PlayerHand, target, fk.ReasonJustMove, sk__guoshi.name, nil, true, player)
    end
  end,
})

return sk__guoshi